package com;

import com.entity.ProductCodeEntity;
import com.entity.ProductCodeModelOfEntity;
import freemarker.template.utility.CollectionUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.stream.Collectors;

public class RunTimeModel {
    public static void main(String[] args) throws Exception {
        ProductCodeModel productCodeModel = new ProductCodeModel();

        Properties properties = readProperties();
        ProductCodeModelOfEntity entity = initEntity(properties);
        if (StringUtils.isEmpty(entity.getTable())) {
            List<Map<String, String>> tables = productCodeModel.getTables(entity);
            if (Objects.nonNull(tables)) {
                tables.stream().map((table) -> {
                    chooseTable(entity, productCodeModel, table.get("TABLE_NAME"));

                    return entity;
                }).collect(Collectors.toList());
            }
        } else if (StringUtils.indexOf(entity.getTable(), ";") > 0) {
            String[] tables = StringUtils.split(entity.getTable(), ";");
            Arrays.stream(tables).map(table -> {
                chooseTable(entity, productCodeModel, table);
                return table;
            }).collect(Collectors.toList());
        } else {
            productCodeModel.choose(entity);
        }
    }

    private static ProductCodeModelOfEntity chooseTable(ProductCodeModelOfEntity entity, ProductCodeModel productCodeModel, String tableName) {
        ProductCodeModelOfEntity target = new ProductCodeModelOfEntity();
        BeanUtils.copyProperties(entity, target);

        target.setTable(tableName);
        target.setChanageDTOPackStr(Boolean.TRUE);

        try {
            productCodeModel.choose(target);

            System.out.println("===================================================================================");
            System.out.println("=======================================" + target.getTable() + "====================================");
            System.out.println("===================================================================================");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return target;
    }

    private static Properties readProperties() throws IOException {
        Properties properties = null;
        FileInputStream in = null;
        try {
            File file = new File("model.properties");
            in = new FileInputStream(file);
            System.out.println(file.getAbsolutePath());
            properties = new Properties();
            properties.load(in);
        } finally {
            IOUtils.closeQuietly(in);
        }

        return properties;
    }

    private static ProductCodeModelOfEntity initEntity(Properties properties) {
        ProductCodeModelOfEntity entity = new ProductCodeModelOfEntity();
        entity.setTable(getProperty(properties, "table"));
        entity.setDbName(getProperty(properties, "dbName"));
        entity.setReplateStr(getProperty(properties, "replateStr"));
        entity.setIp(getProperty(properties, "ip"));
        entity.setUser(getProperty(properties, "user"));
        entity.setPwd(getProperty(properties, "pwd"));
        entity.setBasePath(getProperty(properties, "basePath"));
        entity.setEntityStr(getProperty(properties, "entityStr"));
        entity.setDtoStr(getProperty(properties, "dtoStr"));
        entity.setVoStr(getProperty(properties, "voStr"));
        entity.setMapperStr(getProperty(properties, "mapperStr"));
        entity.setMapperXMLStr(getProperty(properties, "mapperXMLStr"));
        entity.setServiceStr(getProperty(properties, "serviceStr"));
        entity.setServiceImplStr(getProperty(properties, "serviceImplStr"));
        entity.setControllerStr(getProperty(properties, "controllerStr"));
        entity.setFeignStr(getProperty(properties, "feignStr"));
        entity.setStartCharacter(getProperty(properties, "startCharacter"));
        entity.setResultClass(getProperty(properties, "resultClass"));
        entity.setSuccessMethod(getProperty(properties, "successMethod"));
        entity.setErrorMethod(getProperty(properties, "errorMethod"));
        entity.setLocalDateTime(getProperty(properties, "localDateTime"));
        entity.setDateUtils(getProperty(properties, "dateUtils"));
        entity.setJsonUtilMethod(getProperty(properties, "jsonUtilMethod"));
        entity.setVoDtoJsonUtilMethod(getProperty(properties, "voDtoJsonUtilMethod"));
        entity.setLocalDateTimeUtils(getProperty(properties, "localDateTimeUtils"));
        entity.setIsAuthorityURL(getProperty(properties, "isAuthorityURL"));
        entity.setModelDictionName(getProperty(properties,"model_diction_name"));
        entity.setJsPath(getProperty(properties,"jsPath"));
        entity.setPagePath(getProperty(properties,"pagePath"));


        return entity;
    }

    private static String getProperty(Properties properties, String key) {
        return Optional.ofNullable(properties).map(p -> {
            String val = p.getProperty(key);
            return Objects.isNull(val) ? null : StringUtils.trim(val);
        }).orElse(null);
    }
}
